package edu.ustb.aur.userpackage;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import edu.ustb.aur.mapper.UserMapper;
import edu.ustb.aur.utils.PercentageOperate;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author ShiZhe
 * @create 2021-11-03 18:51
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    /**
     * 新建用户
     *
     * @param user
     *            用户对象
     * @return true or false
     */
    @Override
    public boolean createUser(User user) {
        int i = baseMapper.insert(user);
        return i == 1;
    }

    /**
     * 根据用户地址得到user对象
     *
     * @param userAddress
     *            用户hash地址
     * @return user对象
     */
    @Override
    public User findUser(String userAddress) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_address", userAddress);
        return baseMapper.selectOne(wrapper);
    }

    @Override
    public String findUserAddressByUserName(String userName) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_name", userName);
        return baseMapper.selectOne(wrapper).getUserAddress();
    }

    /**
     * 查询用户涉及的资产集合
     *
     * @param userAddress
     *            用户hash地址
     * @return 用户涉及资产集合
     */
    @Override
    public List<String> findAssetList(String userAddress) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_address", userAddress);
        User user = baseMapper.selectOne(wrapper);
        return user.getUserAssetList();
    }

    /**
     * 根据用户名称查询用户是否可以访问
     * @author EddieShi
     * @date 2023/1/3 14:54
     * @param userAddress 用户地址
     * @return boolean true or false
     */
    @Transactional
    @Override
    public boolean findUserLockByUserAddress(String userAddress) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_address", userAddress);
        User user = baseMapper.selectOne(wrapper);
        Integer userLock = user.getUserLock();
        if (userLock == 0) {
            return false;
        }
        user.setUserLock(0);
        baseMapper.updateById(user);
        return true;
    }

    /**
     * 根据用户名称解锁
     *
     * @param userList 用户名称列表
     */
    @Override
    public void updateUserLockByUserAddressList(List<String> userList) {
        for (String userAddress : userList) {
            QueryWrapper<User> wrapper = new QueryWrapper<>();
            wrapper.eq("user_address", userAddress);
            User user = baseMapper.selectOne(wrapper);
            user.setUserLock(1);
            baseMapper.updateById(user);
        }
    }

    /**
     * 资金转出
     *
     * @param userAddress
     *            用户hash地址
     * @param money
     *            金额
     * @return true or false
     */
//    @Transactional
    @Override
    public boolean transferOut(String userAddress, double money) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_address", userAddress);
        User user = baseMapper.selectOne(wrapper);
        if (PercentageOperate.compare(user.getBalance(), money)) {
            Double result = PercentageOperate.subtract(user.getBalance(), money);
            user.setBalance(result);
            baseMapper.updateById(user);
            System.out.println(result);
            return true;
        } else if(PercentageOperate.equal(user.getBalance(), money)) {
            Double result = PercentageOperate.subtract(user.getBalance(), money);
            user.setBalance(result);
            baseMapper.updateById(user);
            System.out.println(result);
            return true;
        } else {
                System.out.println("用户余额不足！！！");
                return false;
        }
    }

    /**
     * 资金转入
     *
     * @param userAddress
     *            用户hash地址
     * @param money
     *            金额
     * @return true or false
     */
//    @Transactional
    @Override
    public boolean transferIn(String userAddress, double money) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_address", userAddress);
        User user = baseMapper.selectOne(wrapper);
        Double result = PercentageOperate.add(user.getBalance(), money);
        user.setBalance(result);
        int i = baseMapper.updateById(user);
        return i == 1;
    }

    /**
     * 添加资产hash集合
     * 
     * @param userAddress
     *            用户hash地址
     * @param assetId
     *            资产hash地址
     * @return true or false
     */
    @Override
    public boolean addAssetList(String userAddress, String assetId) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_address", userAddress);
        User user = baseMapper.selectOne(wrapper);
        List<String> userAssetList = user.getUserAssetList();
        ArrayList<String> userAssetLists = new ArrayList<>(userAssetList);
        if (!userAssetLists.contains(assetId)){
            userAssetLists.add(assetId);
        }
        user.setUserAssetList(userAssetLists);
        int i = baseMapper.updateById(user);
        return i == 1;
    }

    /**
     * 删除资产hash集合
     * 
     * @param userAddress
     *            用户hash地址
     * @param assetId
     *            资产hash地址
     * @return true or false
     */
    @Override
    public boolean deleteAssetList(String userAddress, String assetId) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_address", userAddress);
        User user = baseMapper.selectOne(wrapper);
        List<String> userAssetList = user.getUserAssetList();
        ArrayList<String> userAssetLists = new ArrayList<>(userAssetList);
        userAssetLists.remove(assetId);
        user.setUserAssetList(userAssetLists);
        int i = baseMapper.updateById(user);
        return i == 1;
    }

}
